<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!--loading-->
    <div class="loading" v-if="show">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ articel.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="articel.aut_photo"
        />
        <div class="text">
          <p class="name">{{ articel.aut_name }}</p>
          <p class="time">{{ articel.pubdate | moment }}</p>
        </div>
        <van-button round size="small" type="info" @click="isattention">{{
          articel.is_followed ? '取消关注' : '+关注'
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="articel.content"></div>
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>

      <!--按钮-->
      <div class="btns">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="ZanFn"
        >
          {{ articel.attitude === 1 ? '取消点赞' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete-o"
          @click="islike"
        >
          {{ articel.attitude === 0 ? '取消不喜欢' : '不喜欢' }}
        </van-button>
      </div>
    </div>

    <Comment :articel="articel"></Comment>
  </div>
</template>

<script>
import Comment from './comment.vue'
import { articlecontent, likezan, qzan, like, qlike } from '@/api/article'
import { attention, quattention } from '@/api/user'
import { mapState } from 'vuex'

export default {
  name: 'articel',
  components: {
    Comment
  },
  data() {
    return {
      articel: {},
      show: true
    }
  },
  async created() {
    const res = await articlecontent(this.artid)

    this.articel = res.data

    this.show = false
  },

  methods: {
    async ZanFn() {
      if (this.token.token) {
        try {
          if (this.articel.attitude !== 1) {
            await likezan(this.articel.art_id)
            this.$toast.success('点赞成功')
            this.articel.attitude = 1
          } else {
            await qzan(this.articel.art_id)
            this.$toast.success('取消点赞成功')
            this.articel.attitude = -1
          }
        } catch {}
      } else {
        this.$router.push('/login')
      }
    },

    async islike() {
      if (this.token.token) {
        try {
          if (this.articel.attitude !== 0) {
            await like(this.articel.art_id)
            this.$toast.success('不喜欢成功')
            this.articel.attitude = 0
          } else {
            await qlike(this.articel.art_id)
            this.$toast.success('取消成功')
            this.articel.attitude = -1
          }
        } catch {}
      } else {
        this.$router.push('/login')
      }
    },

    async isattention() {
      if (this.token.token) {
        if (this.articel.is_followed) {
          await quattention(this.articel.aut_id)
          this.$toast.success('取消成功')
          this.articel.is_followed = false
        } else {
          try {
            await attention(this.articel.aut_id)
            this.$toast.success('关注成功')
            this.articel.is_followed = true
          } catch {}
        }
      } else {
        console.log(this.$route)
        this.$router.push({
          path: '/login',
          query: {
            back: this.$route.path
          }
        })
      }
    }
  },
  computed: {
    ...mapState('user', ['token']),
    artid() {
      return this.$route.params.id
    }
  }
}
</script>

<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
  }
}
</style>
